package com.jiahua.admin.biz.action.inventory;

import java.awt.image.BufferedImage;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;

import org.apache.log4j.Logger;

import com.jiahua.admin.biz.service.InventoryCategoryService;
import com.jiahua.admin.biz.service.InventoryItemService;
import com.jiahua.user.biz.model.InventoryCategory;
import com.jiahua.user.biz.model.InventoryItem;
import com.jiahua.utils.string.StringUtil;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionSupport;

public class InventoryItemSaveAction extends ActionSupport {

	private static final long serialVersionUID = -7098230510728400240L;
	Logger log = Logger.getLogger(InventoryItemSaveAction.class.getName());

	InventoryItemService inventoryItemService;
	InventoryCategoryService inventoryCategoryService;

	private String categoryID;
	private String title;
	private String price;
	private String weight;
	private String quantity;
	private String shortDesc;
	private String longDesc;
	private String releaseDate;

	private String productImage;

	private String x1;
	private String y1;
	private String w;
	private String h;

	private File file;

	public String getCategoryID() {
		return categoryID;
	}

	public void setCategoryID(String categoryID) {
		this.categoryID = categoryID;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}

	public String getPrice() {
		return price;
	}

	public void setPrice(String price) {
		this.price = price;
	}

	public String getWeight() {
		return weight;
	}

	public void setWeight(String weight) {
		this.weight = weight;
	}

	public String getQuantity() {
		return quantity;
	}

	public void setQuantity(String quantity) {
		this.quantity = quantity;
	}

	public String getShortDesc() {
		return shortDesc;
	}

	public void setShortDesc(String shortDesc) {
		this.shortDesc = shortDesc;
	}

	public String getLongDesc() {
		return longDesc;
	}

	public void setLongDesc(String longDesc) {
		this.longDesc = longDesc;
	}

	public String getReleaseDate() {
		return releaseDate;
	}

	public void setReleaseDate(String releaseDate) {
		this.releaseDate = releaseDate;
	}

	public File getFile() {
		return file;
	}

	public void setFile(File file) {
		this.file = file;
	}

	public String getProductImage() {
		return productImage;
	}

	public void setProductImage(String productImage) {
		this.productImage = productImage;
	}

	public String getX1() {
		return x1;
	}

	public void setX1(String x1) {
		this.x1 = x1;
	}

	public String getY1() {
		return y1;
	}

	public void setY1(String y1) {
		this.y1 = y1;
	}

	public String getW() {
		return w;
	}

	public void setW(String w) {
		this.w = w;
	}

	public String getH() {
		return h;
	}

	public void setH(String h) {
		this.h = h;
	}

	public void setInventoryItemService(InventoryItemService inventoryItemService) {
		this.inventoryItemService = inventoryItemService;
	}

	public void setInventoryCategoryService(InventoryCategoryService inventoryCategoryService) {
		this.inventoryCategoryService = inventoryCategoryService;
	}

	@Override
	public String execute() throws Exception {

		InventoryItem inventoryItem = new InventoryItem();
		
		InventoryCategory inventoryCategory = inventoryCategoryService.getInventoryCategoryById(categoryID);		
		inventoryItem.setInventoryCategory(inventoryCategory);
		inventoryItem.setTitle(title);
		inventoryItem.setPrice(Double.parseDouble(price));
		inventoryItem.setWeight(Double.parseDouble(weight));
		inventoryItem.setQuantity(Integer.parseInt(quantity));
		inventoryItem.setShortDesc(shortDesc);
		inventoryItem.setLongDesc(StringUtil.regularExpressionsOfHTML(longDesc));
		inventoryItem.setDescription("demo");
		inventoryItem.setRegistrationDate(StringUtil.getRegDate());
		inventoryItem.setWarehousingDate(StringUtil.getRegDate());
		inventoryItem.setUpdateDate(StringUtil.getRegDate());
		inventoryItem.setImage(getByteFile(productImage));
		inventoryItem.setThumb(getByteFile(productImage));
		inventoryItem.setFlag(1);

		inventoryItemService.saveInventoryItem(inventoryItem);
		
		inventoryItemService.listInventoryItem();
		
		return Action.SUCCESS;
	}

	private byte[] getByteFile(String fileName) {
		
		byte[] bFile = null;

		try {
			BufferedImage outImage = ImageIO.read(new File(fileName));
			BufferedImage cropped = outImage.getSubimage(Integer.parseInt(x1), Integer.parseInt(y1),
														 Integer.parseInt(w),  Integer.parseInt(h));

			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			ImageIO.write(cropped, "jpg", baos);

			baos.flush();
			bFile = baos.toByteArray();
			baos.close();

		} catch (IOException e) {
			System.out.println(e.getMessage());
		}
		return bFile;
	}

}
